package io.navigation.modules.navigation.controller;

import java.util.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import io.navigation.common.utils.HttpClientUtil;
import io.navigation.modules.navigation.entity.NavigationClassificationEntity;
import io.navigation.modules.navigation.service.NavigationClassificationService;
import io.navigation.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.navigation.modules.navigation.entity.NavigationEntity;
import io.navigation.modules.navigation.service.NavigationService;
import io.navigation.common.utils.PageUtils;
import io.navigation.common.utils.R;

/**
 * 导航信息表
 *
 * @author sunzhijun
 * @email ,sunzhijunadmin@163.com
 * @date 2019-01-23 20:42:42
 */
@RestController
@RequestMapping("api/navigation/navigation")
@Api(tags = "导航信息表控制类")
public class NavigationController extends AbstractController {
    @Autowired
    private NavigationService navigationService;
    @Autowired
    private NavigationClassificationService navigationClassificationService;

    /**
     * 列表
     */
    @GetMapping("/page")
    @ApiOperation("导航信息表列表")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = navigationService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 根据条件获取导航信息集合
     */
    @GetMapping("/queryList")
    @ApiOperation("根据条件获取导航信息集合")
    public R queryList(@RequestParam Map<String, Object> params){
        params.remove("params");
        List<NavigationEntity> list = navigationService.selectByMap(params);

        return R.ok().put("list", list);
    }

    /**
     * 获取所有分类和导航信息的JSON
     */
    @GetMapping("/queryAllJson")
    @ApiOperation("获取所有分类和导航信息的JSON")
    public R queryAllJson(){
        Map<String, Object> map = new HashMap<>();
        map.put("is_reveal", 1);
//        if (StringUtils.isNotBlank(getUserId())) {
//            map.put("is_private", 1);
//            map.put("user_id", getUserId());
//        } else {
            map.put("is_private", 0);
//        }
        //获取所有可展示分类
        List<NavigationClassificationEntity> classList = this.navigationClassificationService.selectByMap(map);
        //通过递归的方式进行调整并查询出分类下所有可展示的网站，json拼串
        JSONArray jsonArray = findJson(classList, "0");

        return R.ok().put("data", jsonArray);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
//    @RequiresPermissions("navigation:navigation:info")
    @ApiOperation("导航信息表信息")
    public R info(@PathVariable("id") String id){
			NavigationEntity navigation = navigationService.selectById(id);

        return R.ok().put("navigation", navigation);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @RequiresPermissions("navigation:navigation:save")
    @ApiOperation("导航信息表保存")
    public R save(@RequestBody NavigationEntity navigation){
			navigationService.insert(navigation);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @RequiresPermissions("navigation:navigation:update")
    @ApiOperation("导航信息表修改")
    public R update(@RequestBody NavigationEntity navigation){
			navigationService.updateById(navigation);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @RequiresPermissions("navigation:navigation:delete")
    @ApiOperation("导航信息表删除")
    public R delete(@RequestBody String[] ids){
			navigationService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 通过递归的方式进行调整并查询出分类下所有可展示的网站，json拼串
     * @param classList 分类集合
     * @param parentId 父级id
     * @return
     */
    public JSONArray findJson(List<NavigationClassificationEntity> classList, String parentId) {
        JSONArray jsonArray = new JSONArray();
        if (null != classList && 0 < classList.size()) {
            //遍历分类信息
            for (NavigationClassificationEntity nc : classList) {
                //获取分类的父级id为parentId的进行拼串
                if (nc.getParentId().equals(parentId)) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("id", nc.getId());
                    jsonObject.put("icon", nc.getClassIcon());
                    jsonObject.put("parentId", nc.getParentId());
                    jsonObject.put("name", nc.getName());
                    jsonObject.put("sort", nc.getSort());
                    jsonObject.put("description", nc.getDescription());
                    //获取分类下所有网站链接json串
                    JSONArray webJson = queryWebUrlJson(nc.getId());
                    if (null != webJson && 0 < webJson.size()) {
                        jsonObject.put("webJson", webJson);
                    }
                    //获取下级分类进行写入
                    jsonObject.put("subsetJson", findJson(classList, nc.getId()));
                    //数据写入集合
                    jsonArray.add(jsonObject);
                }
            }
        }
        return jsonArray;
    }

    /**
     * 根据分类id获取网站信息并进行json拼串
     * @param classificationId 分类id
     * @return
     */
    public JSONArray queryWebUrlJson (String classificationId) {
        JSONArray jsonArray = new JSONArray();
        Map<String, Object> map = new HashMap<>();
        map.put("is_reveal", 1);
        map.put("classification_id", classificationId);
        List<NavigationEntity> list = this.navigationService.selectByMap(map);
        if (null != list && 0 < list.size()) {
            for (NavigationEntity navigation : list) {
                JSONObject json = new JSONObject();
                json.put("id", navigation.getId());
                json.put("classId", navigation.getClassificationId());
                json.put("name", navigation.getName());
                json.put("description", navigation.getDescription());
                json.put("url", navigation.getUrl());
                json.put("websiteIcon", navigation.getWebsiteIcon());
                json.put("sort", navigation.getSort());

                jsonArray.add(json);
            }
        }
        return jsonArray;
    }

}
